package org.qydjk.console.controller.user;

import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.user.User;
import org.qydjk.common.pojo.user.UserMoneyDetail;
import org.qydjk.common.pojo.user.UserMoneyExtractDetail;
import org.qydjk.common.pojo.user.UserMoneyExtractRecord;
import org.qydjk.common.shiro.vo.ShiroUser;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.IUserWithdrawalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Lanan on 2018/1/10 0010.
 */

@RequestMapping("/user/withdrawals")
@Controller
public class UserWithdrawalsController extends BaseController {

    @Autowired
    private IUserWithdrawalsService userWithdrawalsService;

    @RequestMapping("/index.do")
    public String userWithdrawalsIndex() {
        return "user/withdrawals/userWithdrawals";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object userWithDrawalsList (Integer page, Integer rows) {
        Integer count = 0;
        List<UserMoneyExtractRecord> userMoneyExtractRecords = new ArrayList<UserMoneyExtractRecord>();
        String sql = "select * from qy_user_money_extract_record where 1 = 1";
        String countSql = "select count(id) from qy_user_money_extract_record where 1 = 1";
        String whereSqlStr = " ";
        whereSqlStr = addEqulWhereStr("userId", "user_id");
        whereSqlStr += addEqulWhereStr("type", "type");
        whereSqlStr += addEqulWhereStr("accountType", "account_type");
        sql += whereSqlStr;
        countSql += whereSqlStr;
        // 添加时间条件
        sql += dateHandel();
        countSql += dateHandel();
        sql += " order by update_time desc, create_time desc";
        try {
            userMoneyExtractRecords = baseService.findBySQL(sql, UserMoneyExtractRecord.class, new HibernatePage(page, rows));
            count = baseService.getRecordCountSQL(countSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(count, userMoneyExtractRecords);
    }

    private String dateHandel() {
        String dateWhere = "";
        String startDateStr = request.getParameter("searchStartTemp");
        String endDateStr = request.getParameter("searchEndTemp");
        boolean startdateIsEmpty = !StringUtil.isEmptyOrNull(startDateStr);
        boolean endDateIsEmpty = !StringUtil.isEmptyOrNull(endDateStr);
        if (startdateIsEmpty || endDateIsEmpty) {
            if (endDateIsEmpty) {
                endDateStr += " 23:59:59";
                dateWhere += addDateStr(endDateStr, "<");
            }
            if (startdateIsEmpty) {
                startDateStr += " 00:00:00";
                dateWhere += addDateStr(startDateStr, ">=");
            }
        }
        return dateWhere;
    }

    private String addDateStr(String dateStr, String bj) {
        String whereEndTime = " AND create_time" + bj + "'" + dateStr + "' ";
        return whereEndTime;
    }

    private String addEqulWhereStr(String key, String sqlKey) {
        String whereStr = " ";
        String userId = request.getParameter(key);
        if (!StringUtil.isEmptyOrNull(userId)) {
            whereStr += " and " + sqlKey + " = '" + userId + "' ";
        }
        return whereStr;
    }



    @RequestMapping("/detail/list.do")
    @ResponseBody
    public Object userWithDrawalsDetailList (Integer page, Integer rows) {
        Integer count = 0;
        List<UserMoneyExtractDetail> userMoneyExtractDetails = new ArrayList<UserMoneyExtractDetail>();
        String sql = "select * from qy_user_money_extract_detail where 1 = 1";
        String countSql = "select count(id) from qy_user_money_extract_detail where 1 = 1";
        String whereStr = addEqulWhereStr("extractRecordId", "extract_record_id");
        sql += whereStr;
        countSql += whereStr;
        try {
            userMoneyExtractDetails = baseService.findBySQL(sql, UserMoneyExtractDetail.class, new HibernatePage(page, rows));
            count = baseService.getRecordCountSQL(countSql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(count, userMoneyExtractDetails);
    }


    /**-----------------------------------------------  审核相关 ----------------------------------------------------*/
    /**
     * 拒绝  1.操作金额退回账户余额
     *       2.余额明细加入 记录
     *       3.修改提现申请记录表
     *       4.修改相关余额提现明细表
     * @param id
     * @return
     */
    @RequestMapping("/check/no.do")
    @ResponseBody
    public Object editUserIthdrawlsNO(Integer id, String remark) {
        JSONObject resp = new JSONObject();
        if (StringUtil.isEmptyOrNull(remark)) {
            resp.put("success", false);
            resp.put("msg", "备注不能为空!");
            return resp;
        }
        try {
            return userWithdrawalsService.updateUserIthdrawlsNO(id, remark, getShiroUser());
        } catch (Exception e) {
            return fail(e);
        }
    }

    /**
     * 通过   1. 修改提现申请记录表
     *        2. 修改相关余额提现明细表
     * @param id
     * @return
     */
    @RequestMapping("/check/yes.do")
    @ResponseBody
    public Object editUserIthdrawlsYes(Integer id) {
        try {
            return userWithdrawalsService.updateUserIthdrawlsYes(id, getShiroUser());
        } catch (Exception e) {
            return fail(e);
        }
    }

    /**
     * 完成   1. 修改提现申请记录表
     *        2. 修改相关余额提现明细表
     * @param id
     * @return
     */
    @RequestMapping("/check/finish.do")
    @ResponseBody
    public Object editUserIthdrawlsFinash(Integer id) {
        try {
            return userWithdrawalsService.updateUserIthdrawlsFinash(id, getShiroUser());
        } catch (Exception e) {
            return fail(e);
        }
    }

    private Object fail(Exception e) {
        e.printStackTrace();
        JSONObject resp = new JSONObject();
        resp.put("success", false);
        resp.put("msg", e.getMessage());
        return resp;
    }
}
